<?php
 
 class Registration_Model_Auth extends Zend_Db_Table
    {

        protected $_name="user";
        protected $_primary = 'id';
	    function checkUniqueUid($uid)
	    {
	    	
	        $select = $this->_db->select()
	                            ->from($this->_name,array('uid'))
	                            ->where('uid=?',$uid);
	        $result = $this->getAdapter()->fetchOne($select);
	        
	        return $result;
	        
	    }
	    
    	function checkUniqueEmail($email)
	    {
	    	
	        $select = $this->_db->select()
	                            ->from($this->_name,array('uid'))
	                            ->where('email=?',$email);
	        $result = $this->getAdapter()->fetchOne($select);
	        
	        return $result;
	        
	    }
	    
	    public function insertUser($arrParam = null, $option = null)
	  	{	
	  		$row = $this->fetchNew();		  	
			$row->email       =$arrParam['email'];						
			$row->displayname       =$arrParam['displayname'];
			$row->password       =$arrParam['password'];
			$row->uid       =$arrParam['uid'];
			$row->token = $arrParam['token'];
			$row->phone = $arrParam['phone'];
			$row->packet = $arrParam['packet'];
			$row->link_page_shop = $arrParam['link_page_shop'];
			$row->createdate = date('Y-m-d h:i:s',time());
		  	return $row->save();
	  	}
	  	
    	public function listItem($arrParam = null, $option = null){
	  		
    		$db = Zend_Registry::get('connectDB');
	  		$pagesize = $arrParam['pagesize'];
	  			
  			$offset = ($arrParam['page'] - 1) * $pagesize;
  					
	    	if($arrParam["userid"] != ""){
	  			
	  			$sql = "select * from user where id not  in  (select followed from  follow where follower = ".$arrParam["userid"].") and id <> ".$arrParam["userid"]." limit ".$offset.",".$pagesize;
	  			
	  		}
	  		else{
	
	  			$sql = "select * from user limit ".$offset.",".$pagesize;
	  			
	  		}
	  		
			return $db->fetchAll($sql);
	  	
	  		
	  	}
	  	
	    public function getItemByComment($iduser){
	
	  		$db = Zend_Registry::get('connectDB');		
			$sql = "SELECT *  FROM user where id =  '".$iduser."'";
	  		
			return $db->fetchRow($sql);
	  	
	  		
	  	}
	  	
	  	public function getItemByUsername($email){
	
	  		$db = Zend_Registry::get('connectDB');		
			$sql = "SELECT *  FROM user where email =  '".$email."'";
	  		
			return $db->fetchRow($sql);
	  	
	  		
	  	}
	  	
    	public function getItemById($id){
	
	  		$db = Zend_Registry::get('connectDB');		
			$sql = "SELECT *  FROM user where id =  '".$id."'";
	  		
			return $db->fetchRow($sql);
	  	
	  		
	  	}
	  	
    	public function getItemByUid($uid){
	
	  		$db = Zend_Registry::get('connectDB');		
			$sql = "SELECT *  FROM user where uid =  '".$uid."'";
	  		
			return $db->fetchRow($sql);
	  	
	  		
	  	}
	  	
	    public function findItem($val){
	  		$db = Zend_Registry::get('connectDB');
	  		$sql = "select * from user where interest LIKE '%".$val."%'";
	  		$result = $db->fetchAll($sql);
			
	  		return $result;
	  	}
	  	
	  	
    	// update Profile user
		public function updateProfile($arrParam = null, $option = null)
		{
			$db = Zend_Registry::get('connectDB');	
			$where = "id = '".$arrParam['id']."'";

			$data = array();
			
			if (@$arrParam['displayname'] != ""){
				$data["displayname"]=$arrParam['displayname'];
			}
			
			if (@$arrParam['password'] != ""){
				$data["password"]=$arrParam['password'];
			}
			
			if (@$arrParam['token'] != ""){
				$data["token"]=$arrParam['token'];
			}
			
			if (@$arrParam['email'] != ""){
				$data["email"]=$arrParam['email'];
			}
			
			if (@$arrParam['phone'] != ""){
				$data["phone"]=$arrParam['phone'];
			}
			
			if (@$arrParam['address'] != ""){
				$data["address"]=$arrParam['address'];
			}
			if (@$arrParam['expire_date'] != ""){
				$data["expire_date"]=$arrParam['expire_date'];
			}
						
			$data["last_update"]= date('Y-m-d h:i:s',time());
						
			$db->update("user", $data, $where);
		}

    public function checkLoginCookie(){
		
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		
		if($data){
				
			return true;
			
		}
		
		$request = new Zend_Controller_Request_Http();
  		$email_val = $request->getCookie("uthietdep");
  		$password_val = $request->getCookie("pthietdep");
		
  		if($email_val && $password_val){
  			
  			$email = $this->getEmailDecrypt($email_val);
  			$password = $this->getPasswordDecrypt($password_val);
  			
  			$user = $this->getItemByUsername($email);
  					
			//login
  			$registration  = new Registration_AuthController();
  			$registration->login($email,$password);
			return true;				
  			  			
  		}
  		
  		return false;
  		
	}
		

	public function checkLoginCookieToken(){
		
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		
		if($data){
			
			Cu_Facebook::setAccessToken($data->token);
  				
  			$user = Cu_Facebook::getUser();
	
			if ($user){
				
				return true;
				
			}
			else{
				
				return false;
				
			}
			
		}
		
		$request = new Zend_Controller_Request_Http();
  		$email_val = $request->getCookie("uthietdep");
  		$password_val = $request->getCookie("pthietdep");
		
  		if($email_val && $password_val){
  			
  			$email = $this->getEmailDecrypt($email_val);
  			$password = $this->getPasswordDecrypt($password_val);
  			
  			$user = $this->getItemByUsername($email);
  			
  			if($user){
  				
  				//check token time
  				
  				Cu_Facebook::setAccessToken($user["token"]);
  				
  				$user = Cu_Facebook::getUser();
	
				if ($user) {
  					
					return true;
				} 
  				
  			}
  		}
  		
  		return false;
  		
	}
	
	public function removeAccountCookie(){
		
		$request = new Zend_Controller_Request_Http();
  		$email = $request->getCookie("uthietdep");
  		$password = $request->getCookie("pthietdep");
  		
  		if($email){

  			setcookie("uthietdep",time() - 86400 * 365 * 2);
  		}
  		
  		if($password){

  			setcookie("pthietdep",time() - 86400 * 365 * 2);
  		}
  				
	}
	
    public function getEmailDecrypt($email_val){
    	
    	$request = new Zend_Controller_Request_Http();
  		
  		$email = $this->decrypt($email_val);
  		
  		return $id;
  		  		
    }
    
    public function getPasswordDecrypt($password_val){
    	
  		$password = $this->decrypt($password_val);
  		
  		return $password;
  		
    }
	    
    public function encrypt($val){
    	
    	
    	$zrsa =new Zend_Crypt_Rsa(array("passPhrase"=>"otaha@123asd!@#up","pemPath"=>realpath(APPLICATION_PATH . "/public/key/rsa"))); 
     	
    	
     	$enc = $zrsa->encrypt($val, $zrsa->getPublicKey(),Zend_Crypt_Rsa::BASE64);
     	
     	return $enc;
     	
		
    }
    
    public function decrypt($enc){

    	$zrsa =new Zend_Crypt_Rsa(array("passPhrase"=>"otaha@123asd!@#up","pemPath"=>realpath(APPLICATION_PATH . "/public/key/rsa")));
     	$dec = $zrsa->decrypt($enc, $zrsa->getPrivateKey(),Zend_Crypt_Rsa::BASE64);
		return $dec;
    }
		

    }

?>